#![allow(clippy::too_many_arguments)]

use sqlx::FromRow;

use crate::SQLX;

#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, FromRow)]
pub struct SysRole {
    pub role_id: Option<i64>,
    pub role_name: Option<String>,
    pub parent_id: Option<i64>,
    pub create_time: Option<String>,
    pub state: Option<i64>,
    pub org_id: Option<i64>,
}

impl SysRole {
    pub async fn query_role(user_id: Option<i64>, org_id: Option<i64>) -> anyhow::Result<Option<SysRole>> {
        let db = SQLX.get().ok_or(anyhow::anyhow!("db error"))?;
        let sys_role = sqlx::query_as::<_, SysRole>(
            r#"select a.* from 
                sys_role a left join sys_user_role b on a.role_id = b.role_id
                where b.user_id = ? and org_id= ?"#)
            .bind(user_id)
            .bind(org_id).fetch_optional(db).await?;
        Ok(sys_role)
    }
}